﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FySystem.Exceptions
{
    public class FyExceptionManager
    {
        /// <summary>
        /// 
        /// </summary>
        internal static ConcurrentDictionary<EnumCultures, ConcurrentDictionary<string, string>> ExceptionMap = new ConcurrentDictionary<EnumCultures, ConcurrentDictionary<string, string>>();

        /// <summary>
        /// 添加多国语言报错字典
        /// </summary>
        /// <param name="culture"></param>
        /// <param name="exceptionMap"></param>
        public static void AddCulture(EnumCultures culture,ConcurrentDictionary<string,string> exceptionMap)
        {

        }

        public static string GetError(string key, EnumCultures culture)
        {
            string res = key;
            try
            {
                if (string.IsNullOrWhiteSpace(key))
                    return FormatError(res);
                if (ExceptionMap.ContainsKey(culture) == false)
                    return FormatError(res);

                ConcurrentDictionary<string, string> tmp = null;
                if(ExceptionMap.TryGetValue(culture, out tmp))
                {
                    string val = string.Empty;
                    if(tmp!=null)
                    {
                        if(tmp.TryGetValue(key, out val))
                        {
                            res = val;
                        }
                    }
                }

                FormatError(res);
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        private static string FormatError(string error)
        {
            try
            {
                if (error.EndsWith('~') == false)
                    error += '~';
            }
            catch (Exception)
            {
                throw;
            }
            return error;
        }
    }
}
